package com.yanqu.road.server.cmd.banquet;

import com.yanqu.road.entity.banquet.UserBanquetData;
import com.yanqu.road.entity.banquet.UserBanquetMember;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

//@Cmd(code = Protocol.S_BANQUET_SERVER_JOIN, desc = "加入宴会")
public class JoinBanquetCmd extends AbstractCommand {

	private String banquetId;
	private int giftId;

	private String reward = "";
	private int pos = 0;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BanquetProto.JoinBanquetReq reqMsg = BanquetProto.JoinBanquetReq.parseFrom(packet.getBytes());
		banquetId = reqMsg.getBanquetId();
		giftId = reqMsg.getGiftId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		UserBanquetData banquet = BanquetMgr.getBanquet(banquetId);
		if(banquet == null){
			ret = GameErrorCode.E_BANQUET_IS_OVER;
			return;
		}
		if(BanquetConfigMgr.isServerNormalBanquet(banquet.getType()) && !SystemOpenMgr.systemOpen(player, eSystemId.Banquet.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		if(BanquetConfigMgr.isServerFeastBanquet(banquet.getType()) && !SystemOpenMgr.systemOpen(player, eSystemId.BanquetServer.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}
		ret = BanquetMgr.joinBanquet(player,banquetId,giftId);
		if(ret == 0){
			UserBanquetMember userBanquetMember = BanquetMgr.getBanquetMember(banquetId,player.getUserId());
			pos = userBanquetMember.getPos();
			reward = BanquetConfigMgr.getJoinReward(pos,giftId);
			BanquetMgr.addBanquetJoinTimes(player);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		BanquetProto.JoinBanquetResp.Builder respMsg = BanquetProto.JoinBanquetResp.newBuilder();
		respMsg.setRet(ret);
		respMsg.setReward(reward);
		respMsg.setPos(pos);
		player.sendPacket(Protocol.U_BANQUET_SERVER_JOIN, respMsg);
	}
}
